﻿using System;
using GameEngine.Model;
using GameEngine.Model.Data;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.View.Components;

namespace GameEngine.Controller
{
    public class CreateThingCommand<MyIdType, MyBaseUIElementType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            var thing = notification.Body as IThingVO<MyIdType>;
            var worldIdValue = notification.Type;
            var worldDataProxyName = IWorldDataProxyConstants<MyIdType>.CreateDataProxyName(worldIdValue);
            var worldDataProxy = Facade.RetrieveProxy(worldDataProxyName) as IWorldDataProxy<MyIdType>;

            var thingDataProxy = ModelFacade<MyIdType>.Instance.CreateThingDataProxy(thing);
            Facade.RegisterProxy(thingDataProxy);

            var viewInstance = ViewFacade<MyIdType, MyBaseUIElementType>.Instance.CreateViewInstance();
            var worldThingMediator = ViewFacade<MyIdType, MyBaseUIElementType>.Helper.GetWorldThingMediator(worldDataProxy.Id);
            worldThingMediator.AddThing(thingDataProxy, viewInstance);
        }
    }
}
